package com.kingsalert.kingsalert.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.bean.PermissionInfo;
import com.kingsalert.kingsalert.listener.PermissionListener;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.util.ArrayList;


public class PermissionViewAdapter extends RecyclerView.Adapter<PermissionViewAdapter.PermissionDialogHolder> {

    private final Context context;

    private final ArrayList<PermissionInfo> infos;

    public PermissionViewAdapter(Context context, ArrayList<PermissionInfo> infos){
        this.context = context;
        this.infos = infos;
    }

    public PermissionInfo getInfo(int index){
        return infos.get(index);
    }

    @NonNull
    @Override
    public PermissionDialogHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PermissionDialogHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_permission,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull PermissionDialogHolder holder, int position) {
        PermissionInfo info = infos.get(position);

        info.setHolder(holder);

        if (info.getIconResId()!=null) holder.setIcon(info.getIconResId());
        holder.setTitle(info.getTitle());
        holder.setText(info.getText());
        holder.setPage(info.getPage());

        holder.setListener(info.getListener());
    }

    @Override
    public int getItemCount() {
        return infos.size();
    }

    public static class PermissionDialogHolder extends RecyclerView.ViewHolder{

        private final View mView;

        private final ImageView imgIv;
        private final  TextView titleTv;
        private final  TextView textTv;
        private final  ImageButton checkIb;

        private final View checkLayout;

        private PermissionListener listener;

        private boolean check = false;

        @SuppressLint("InflateParams")
        public PermissionDialogHolder(View view){

            super(view);
            mView = view;

            imgIv = view.findViewById(R.id.item_permission_iv_icon);
            titleTv = mView.findViewById(R.id.item_permission_tv_title);
            textTv = mView.findViewById(R.id.item_permission_tv_text);
            checkIb = mView.findViewById(R.id.itpe_ib_check);

            checkLayout = mView.findViewById(R.id.itpe_layout_check);

        }

        public void setIcon(int iconresid){
            imgIv.setImageResource(iconresid);
        }

        public void setTitle(String title) {
            titleTv.setText(title);
        }

        public void setText(String text){
            textTv.setText(text);
        }

        public void setCheck(boolean check) {
            this.check = check;
            checkIb.setBackgroundResource(check ? R.drawable.icon_permission_agreement_check_true :R.drawable.icon_permission_agreement_check_false);
        }

        public boolean getCheck() {
            return check;
        }

        public void setListener(PermissionListener listener) {
            this.listener = listener;

            mView.findViewById(R.id.item_permission_btn_deny).setOnClickListener(v -> {
                if (listener!=null) listener.onPermissionDeny();
            });

            mView.findViewById(R.id.item_permission_btn_agree).setOnClickListener(v -> {
                if (listener!=null) listener.onPermissionAgree();
            });

        }

        public void afterAgree(){
            mView.findViewById(R.id.item_permission_view_ok).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.item_permission_view_btn).setVisibility(View.GONE);
            checkLayout.setVisibility(View.GONE);
        }

        public void setPage(String page){
            checkIb.setOnClickListener(v -> {
                setCheck(!check);
                HttpUtil.getInstance().saveBuriedPoint(page,check ? "CHECKBOX_YES_CLICK":"CHECKBOX_NO_CLICK","");
            });
        }

    }
}
